package com.zxh.zhenxihuan.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zxh.zhenxihuan.entity.Goods;
import com.zxh.zhenxihuan.mapper.GoodsMapper;
import com.zxh.zhenxihuan.service.GoodService;
import com.zxh.zhenxihuan.util.FileUtil;
import com.zxh.zhenxihuan.util.Page;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
public class GoodsController {
    @Resource
    private GoodService goodService;
    @Resource
    private GoodsMapper goodsMapper;

    /**
     *商品查看所有
     * @return
     */
    @PostMapping("/findAll")
    public String findAll(@RequestBody Map maps){
        try {
            Integer num= (Integer) maps.get("page");
            Integer count= (Integer) maps.get("limit");
            String productame= (String) maps.get("productame");
            System.out.println(productame+"..................");
            Integer importanceOptions= (Integer) maps.get("importanceOptions");
            Page<Goods> pages=goodService.page(num,count,productame,importanceOptions);
            Map<String,Object> map=new HashMap<>();
            map.put("page",pages);
            return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd",SerializerFeature.DisableCircularReferenceDetect);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 商品删除
     * @return
     */
    @PostMapping("/goodsDel")
    public String GoodsDel(@RequestBody Map maps){
        //System.out.println("/del");
        Integer id=(Integer) maps.get("id");
        Integer num=goodService.del(id);
        Map<String,Object> map=new HashMap<>();
        if(num>0){
            System.out.println("刪除成功");
        }
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd HH:mm:ss",SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 上传
     */
    @GetMapping(value ="/upload",produces="text/javascript;" +
            " charset=utf-8")
    public String configUpload(HttpServletRequest req,
                               HttpServletResponse res) throws IOException {
        String action = req.getParameter("action");
        if(action!=null){
            String callback = req.getParameter("callback");
            String s = FileUtil.readFileAsString("c://ueditorconfig.txt");
            return callback+"("+s+")";
        }
        return null;
    }

    @PostMapping("/upload")
    public Map handleFileUpload(String token,@RequestParam("file") MultipartFile file) {
        String oldFileName = file.getOriginalFilename();
        int lastDotIndex = oldFileName.lastIndexOf(".");
        String extName = oldFileName.substring(lastDotIndex);
        String newName =  UUID.randomUUID()+extName;
        String os = System.getProperty("os.name");

        File excelFile = new File((os.startsWith("Windows")
                ?"c://":"/upload/") + newName);
        try {
            file.transferTo(excelFile);
            Map map = new HashMap();
            map.put("original",oldFileName);
            map.put("size",file.getSize());
            map.put("state", "SUCCESS");
            map.put("title", newName);
            map.put("type", extName);
            map.put("url", "/find_img?fileName="+newName+"&token="+token);
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/findInventory")
    public int[] findInventory(){
        int[] arr=goodsMapper.findInventory();
        System.out.println("findInventory");
        return arr;
    }

    @PostMapping("/findproName")
    public String[] findproName(){
        String[] arr=goodsMapper.findproName();
        System.out.println("findproName");
        return arr;
    }

    /**
     * 商品添加
     * @return
     */
    @PostMapping("/goodsAdd")
    public Integer GoodsAdd(@RequestBody Goods goods){
        System.out.println("goodsAdd");
        return goodService.add(goods);
        }

}
